package com.lyx.crm.controller;

import com.lyx.crm.annotation.RequiredPermission;
import com.lyx.crm.base.BaseController;
import com.lyx.crm.base.ResultInfo;
import com.lyx.crm.query.CusDevPlanQuery;
import com.lyx.crm.service.CusDevPlanService;
import com.lyx.crm.service.SaleChanceService;
import com.lyx.crm.vo.CusDevPlan;
import com.lyx.crm.vo.SaleChance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Package: com.lyx.crm.controller
 * @ClassName: CusDevPlanController
 * @Author: lyx
 * @CreateTime: 2021/7/26 19:55
 * @Description:
 */

@Controller
@RequestMapping("cus_dev_plan")
public class CusDevPlanController extends BaseController {

    @Autowired
    private SaleChanceService saleChanceService;
    @Autowired
    private CusDevPlanService cusDevPlanService;

    /**
     * 进入客户开发计划页面
     *
     * @return
     */
    @RequiredPermission(code = "1020")
    @RequestMapping("index")
    public String index() {
        return "cusDevPlan/cus_dev_plan";
    }

    /**
     * 打开计划项开发详情页面
     *
     * @return
     */
    @RequiredPermission(code = "102001")
    @RequestMapping("toCusDevPlanPage")
    public String toCusDevPlanPage(Integer id, HttpServletRequest request) {
        SaleChance saleChance = saleChanceService.selectByPrimaryKey(id);

        request.setAttribute("saleChance", saleChance);

        return "cusDevPlan/cus_dev_plan_data";
    }

    /**
     * 分页多条件查询
     * 如果flag不为空，这查询的开发计划，如果flag为空，就是查询营销机会
     *
     * @param cusDevPlanQuery
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> querySaleChanceByParams(CusDevPlanQuery cusDevPlanQuery) {
        return cusDevPlanService.querySaleChanceByParams(cusDevPlanQuery);
    }

    /**
     * 添加计划项
     *
     * @param cusDevPlan
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public ResultInfo addCusDevPlan(CusDevPlan cusDevPlan) {
        cusDevPlanService.addCusDevPlan(cusDevPlan);
        return success("计划项添加成功！");
    }

    /**
     * 更新计划项
     *
     * @param cusDevPlan
     * @return
     */
    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateCusDevPlan(CusDevPlan cusDevPlan) {
        cusDevPlanService.updateCusDevPlan(cusDevPlan);
        return success("计划项修改成功！");
    }


    /**
     * @param sId     营销机会id
     * @param request
     * @param id      计划项id
     * @return
     */
    @RequestMapping("toAddOrUpdateCusDevPlanPage")
    public String toAddOrUpdateCusDevPlanPage(HttpServletRequest request, Integer sId, Integer id) {
        // 将营销机会ID设置到请求域中，给计划项页面获取
        request.setAttribute("sId", sId);

        // 通过计划项ID查询记录
        CusDevPlan cusDevPlan = cusDevPlanService.selectByPrimaryKey(id);
        // 将计划项数据设置到请求域中
        request.setAttribute("cusDevPlan", cusDevPlan);
        return "cusDevPlan/add_update";
    }

    /**
     * 删除计划项
     *
     * @param id 计划项id   cus_dev_plan
     * @return
     */
    @ResponseBody
    @RequestMapping("delete")
    public ResultInfo deleteCusDevPlan(Integer id) {
        cusDevPlanService.deleteCusDevPlan(id);
        return success("计划项删除成功");
    }
}
